﻿/////////////////////////////////////////////////////////////////////
//
//	Map_FVG C# Class Library.
//
//	AggiornaSimboli
//	
//
//	autore: Gustavo Mainardi 
//	versione: 1.0
//	data: 01/12/2015
//
//
//	Cronologia delle versioni:
//
//	Versione 1.0 01/12/2015
//		versione originale
//
//////////////////////////////////////////////////////////////////////////
using System.Drawing;
using System.Data.SQLite;

namespace Map_Fvg
{
    public partial class AggiornaSimboli 
    {
        public AggiornaSimboli()
        {
            
        }

        public void aggiornaSimboli1()
        {
            for (int i = 0; i < ViewQ.nSimb; ++i)
            {
                if (ViewQ.vectS[i].selected == 0) continue;
                string ss = ViewQ.vectS[i].codice.ToString();

                using (SQLiteConnection conn = SetConnection())
                {
                    conn.Open();
                    using (SQLiteCommand cmd = new SQLiteCommand(conn))
                    {
                        cmd.CommandText = "select * from linea where codice_stclasse = '" + ss + "'";

                        SQLiteDataReader reader = cmd.ExecuteReader();
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                ViewQ.vectS[i].penType = int.Parse(reader.GetString(3).ToString());
                                ViewQ.vectS[i].tratto = reader.GetString(4).ToString();

                                string[] words;
                                char[] delimiterChars = { ' ', ',' };

                                string s1 = reader.GetString(5).ToString();
                                words = s1.Split(delimiterChars);
                                byte b1 = byte.Parse(words[0]);
                                byte b2 = byte.Parse(words[1]);
                                byte b3 = byte.Parse(words[2]);
                                byte b4 = byte.Parse(words[3]);
                                ViewQ.vectS[i].icolor = Color.FromArgb(b1, b2, b3, b4);
                            }
                        }
                    }
                    conn.Close();
                }

            }

            for (int i = 0; i < ViewQ.nSimbT; ++i)
            {
                if (ViewQ.vectT[i].selected == 0) continue;
                string ss = ViewQ.vectT[i].codice.ToString();

                using (SQLiteConnection conn = SetConnection())
                {
                    conn.Open();
                    using (SQLiteCommand cmd = new SQLiteCommand(conn))
                    {
                        cmd.CommandText = "select * from testo where codice_testo = '" + ss + "'";

                        SQLiteDataReader reader = cmd.ExecuteReader();
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                string s1 = reader.GetString(2).ToString();
                                ViewQ.vectT[i].iFont = stringToFont(s1);

                                ViewQ.vectT[i].altezza = float.Parse(reader.GetString(4).ToString());

                                string[] words;
                                char[] delimiterChars = { ' ', ',' };

                                s1 = reader.GetString(3).ToString();
                                words = s1.Split(delimiterChars);
                                byte b1 = byte.Parse(words[0]);
                                byte b2 = byte.Parse(words[1]);
                                byte b3 = byte.Parse(words[2]);
                                byte b4 = byte.Parse(words[3]);
                                ViewQ.vectT[i].icolor = Color.FromArgb(b1, b2, b3, b4);
                            }
                        }
                        conn.Close();
                    }
                }
            }


            for (int i = 0; i < ViewQ.nSimbA; ++i)
            {
                if (ViewQ.vectA[i].selected == 0) continue;
                string ss = ViewQ.vectA[i].codice.ToString();

                using (SQLiteConnection conn = SetConnection())
                {
                    conn.Open();
                    using (SQLiteCommand cmd = new SQLiteCommand(conn))
                    {
                        cmd.CommandText = "select * from area where codice_area = '" + ss + "'";

                        SQLiteDataReader reader = cmd.ExecuteReader();
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                string s1 = reader.GetString(2).ToString();
                                ViewQ.vectA[i].codice_bordo = int.Parse(reader.GetString(4).ToString());
                                ViewQ.vectA[i].bordo = reader.GetString(5).ToString();
                                ViewQ.vectA[i].codice_camp = int.Parse(reader.GetString(2).ToString());
                                ViewQ.vectA[i].campitura = reader.GetString(3).ToString();


                                s1 = reader.GetString(6).ToString();
                                string[] words;
                                char[] delimiterChars = { ' ', ',' };

                                words = s1.Split(delimiterChars);
                                byte b1 = byte.Parse(words[0]);
                                byte b2 = byte.Parse(words[1]);
                                byte b3 = byte.Parse(words[2]);
                                byte b4 = byte.Parse(words[3]);
                                ViewQ.vectA[i].icolor = Color.FromArgb(b1, b2, b3, b4);
                            }
                        }
                        conn.Close();
                    }
                }
            }

            for (int i = 0; i < ViewQ.nSimbP; ++i)
            {
                //           if (ViewQ.vectP[i].selected == 0) continue;
                string ss = ViewQ.vectP[i].codice.ToString();

                using (SQLiteConnection conn = SetConnection())
                {
                    conn.Open();
                    using (SQLiteCommand cmd = new SQLiteCommand(conn))
                    {
                        cmd.CommandText = "select * from point where codice_point = '" + ss + "'";

                        SQLiteDataReader reader = cmd.ExecuteReader();
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                ViewQ.vectP[i].simbolo = int.Parse(reader.GetString(2).ToString());

                                ViewQ.vectP[i].paramSimb = reader.GetString(3).ToString();

                                string s1 = reader.GetString(4).ToString();
                                s1 = s1.Trim();
                                if (s1.Length > 0) ViewQ.vectP[i].fileName = s1;

                                ViewQ.vectP[i].dimensione = float.Parse(reader.GetString(5).ToString());
                                s1 = reader.GetString(6).ToString();
                            }
                        }
                        conn.Close();
                    }
                }
            }
        }


        private SQLiteConnection SetConnection()
        {
            string DataSource = "Data Source=";
            DataSource = string.Concat(DataSource, FormIniz.cartellaSimb, ";");
            SQLiteConnection conn = new SQLiteConnection(DataSource);
            return (conn);
        }

        private Font stringToFont(string ss)
        {
            float sizef = 1.0F;
            int ipos = ss.IndexOf("Size=", 0);
            if (ipos > 0)
            {
                int ipos1 = ss.IndexOf(",", ipos);
                int len = ipos1 - ipos - 5;
                string s1 = ss.Substring(ipos + 5, len);
                sizef = float.Parse(s1);
                //              MessageBox.Show(s1 + "  " + sizef.ToString()); 
            }

            Font ifont = new Font(ss, sizef);
            return (ifont);
        }

    }
}
